<?php

namespace App\Http\Controllers;

use App\CountryModel;
use App\LanguageModel;
use App\MovieModel;
use App\TypeModel;
use http\Message;
use Illuminate\Http\Request;

class MovieController extends Controller
{

    public function addmovie()
    {
        $types = TypeModel::get();
        $countrys = CountryModel::get();
        $languages = LanguageModel::get();
        return view("admin.addmovie", compact("types", "countrys", "languages"));
    }


    public function movie()
    {
        $cols = ["movie.*", "type.type_ch as typename", "country.country_ch as countryname", "language.language_ch as language"];
        $movies = MovieModel::leftjoin("type", "movie.type", "type.id")->leftjoin("country", "movie.country", "country.id")->leftjoin("lang", "movie.lang", "language.id")->get($cols);
        return view("admin.movies", compact("movies","cols"));
    }


    public function deletetmovie(Request $request)
    {

        try {
            $id = $request->input("id");
            $ok = MovieModel::where("id", $id)->delete();
            if ($ok) {
                $result = ["title" => "记录被删除", "err" => 0];
                $result = json_encode($result);
                return $result;
            } else {
                $result = ["title" => "删除失败", "err" => 1];
                $result = json_encode($result);
                return $result;
            }
        } catch (\Exception $e) {
            dd($e->getLine() . "********************************************" . $e->getMessage());
        }


    }


    public function updatemovie(Request $request)
    {

        $types = TypeModel::get();
        $countrys = CountryModel::get();
        $languages = LanguageModel::get();
        $id = $request->input("id");
        $movie = MovieModel::where("id", $id)->first();
        return view("admin.updatemovie", compact("movie", "types", "countrys", "languages"));

    }


    public function insertmovie(Request $request)
    {

        $data = $request->input();
        $id = $data['id'];
        $files = $request->file();
//     dd($data,$files);
        $image = $files['image'];
        $movie = $files['movie'];
//      dd($image,$movie);

        if ($id == 0) {

            if ($image) {
                $data['image'] = $this->fileupload($image, "image");
            }


            if ($movie) {
                $data['movie'] = $this->fileupload($movie, "movie");
            }
        } else {

            $edtmovie = MovieModel::find($id);
            if ($request->hasFile("image")) {
                $image = $request->file("image");
                $data['image'] = $this->fileupload($image, "image");
                if (file_exists(public_path($edtmovie->image))) {
                    unlink(public_path($edtmovie->image));
                }
            }

            if ($request->hasFile("movie")) {
                $movie = $request->file("movie");
                $data['movie'] = $this->fileupload($movie, "movie");
                if (file_exists(public_path($edtmovie->movie))) {
                    unlink(public_path($edtmovie->movie));
                }

            }
        }
        if ($id == 0) {
            $ok = MovieModel::create($data);
//        dd($ok);
            $result = ["title" => "数据添加成功", "err" => 0];
            $result = json_encode($result);
            return $result;
        } else {
            $ok = MovieModel::find($id);
            $ok->update($data);
            $result = ["title" => "数据修改成功", "err" => 0];
            $result = json_encode($result);
            return $result;
        }


    }


    public function kino(Request $request)
    {
        $cols = ["movie.*", "type.type_ch as typename", "country.country_ch as countryname", "language.language_ch as language"];
        $movie = MovieModel::leftjoin("type", "movie.type", "type.id")->leftjoin("country", "movie.country", "country.id")->leftjoin("language", "movie.lang", "language.id")->get();
        return view("admin.movie", compact("movie", "cols"));
    }

    public function fileupload($file, $filetype)
    {
        $filename = $filetype . "_" . date("ymdhis") . "_" . rand(1, 10000) . "." . $file->getClientOriginalExtension();
        $file->move(public_path("$filetype/"), $filename);
        return $filetype . "/" . $filename;
    }


//    public function front(Request $request)
//    {
//        $data=MovieModel::get();
//        return view("user.index",compact("data"));
//   }
}
